<?php
if(!defined('IN_WEB')) {
    exit('Access Denied');
}

class ProductController extends Controller{
    function __construct(){
        parent::__construct();
        $this->productAction = new ProductAction();
        $this->taxonomyAction = new TaxonomyAction();
    }

    function index(){
        $tid = intval($_GET['tid']);
        $tagIsPlay = false;
        if($tid){
            $conditions = array('tids' => $tid);
            $products = $this->productAction->productListByTids($conditions);
            $term = $this->taxonomyAction->getTerm($tid);
            //一级分类的显示所有一级term，二级以下分类显示同级分类的所有term
            if($term['parent']){
                $term_first = $this->taxonomyAction->getTerm($term['parent']);
                $parent = $term['parent'];
                //增加第Tag的数据，也就是第三级的数据
                $tagsTerm = $this->taxonomyAction->getPtypeTags($tid);
                $tagIsPlay = true;
            }else{
                $parent = $term['tid'];
                $term_first = $term;
            }
            $term_first['class'] = 'first';
            
            $terms = $this->taxonomyAction->termList($term['vid'], $parent);
            array_unshift($terms, $term_first);
        }else{
            $vac = $this->taxonomyAction->getVocabularyByMachineName(TERM_MACHINE_NAME_PTYPE);
            $terms = $this->taxonomyAction->termList($vac['vid'], 0);

            $products = $this->productAction->productListFull();

        }
        //加载颜色标签
        $colors = $this->taxonomyAction->getColorTerms();
        $this->smarty->assign('J_tid', $tid);
        $this->smarty->assign('terms', $terms);
        $this->smarty->assign('products', $products);
        $this->smarty->assign('tagsTerm', $tagsTerm);
        $this->smarty->assign('colors', $colors);
        $this->smarty->assign('tagIsPlay', $tagIsPlay);
        $this->smarty->display('portal/product/index.tpl');
    }
    
    function term(){
        $this->index();
    }
    
    function ajax_data(){
        $tid = intval($_POST['tid']);
        $page = intval($_POST['page']);
        $count = intval($_POST['count']);
        $term = $this->taxonomyAction->getTerm($tid);
        /*//测试用数据
        $tid = intval($_REQUEST['tid']);
        $page = intval($_REQUEST['page']);
        $count = intval($_REQUEST['count']);*/
        
        $conditions = array(
            'tids' => $tid,
            'page' =>  $page,
            'count' => $count,
        );
        if($tid){
            $products = $this->productAction->productListByTids($conditions);
        }else{
            $products = $this->productAction->productListFull($conditions);
        }
        
        $data = array();
        foreach($products as $p){
            $row = array(
            'pid' => $p['pid'],
            'title' => $p['title'],
            'description' => $p['description'],
            'nick' => $p['nick'],
            'url' => $p['url'],
            'shop_url' => $p['shop_url'],
            'sales' => $p['sales'],
            'price' => $p['price'],
            'discount' => $p['discount'],
            'fav' => $p['fav'],
            'ptype_third_tid' => $p['terms']['ptype_third']['tid'],
            'ptype_third_tname' => $p['terms']['ptype_third']['name'],
            'source' => $p['terms']['source']['name'],
            'color_tid' => $p['terms']['color']['tid'],
            'color_tname' => $p['terms']['color']['name'],
            'thumbnail' => $p['images']['thumbnail']['path'],
            'thumbnail_height' => $p['images']['thumbnail']['height'],
            'image' => $p['images']['image']['path'],
            );
            $data[] = $row;
        }
        echo json_encode($data);
    }

    //查询数据库，关联关键字的结果
    function search(){
        $pWord = $_POST['searchWord'];
        $hasResult = true;
        if(trim($pWord) !=""){
            $productListSearch = $this->productAction->productListSearch($pWord);
            if(count($productListSearch) == 0)    {
                $hasResult = false;
            }
            $this->smarty->assign('productListSearch', $productListSearch);
            $this->smarty->assign('hasResult', $hasResult);
        }
        else{
            $hasResult = false;
            $this->smarty->assign('hasResult', $hasResult);
	}
 $ptype_first = $this->taxonomyAction -> getPtypeFirst();
	    foreach($ptype_first as $k=>$t){
	        $first_word = chinese_substr($t['name'],0,1);
	        $from = strlen($first_word);
	        $other_words = chinese_substr($t['name'],$from);
	        
	        $t['new_name'] = '<span class="font20 fn-color-'.$t['tid'].'">'.$first_word.'</span>'.$other_words;
	        $ptype_first[$k] = $t;
	    }
	    $this->smarty->assign('ptype_first', $ptype_first);
        $this->smarty->display('portal/product/search.tpl');
    }
    
    /**
     * 喜爱产品接口
     * index.php?ctrl=product&act=add_fav
     * 参数：
     * $_POST['uid']用户id
     * $_POST['pid']产品id
     * 返回值：
     * -1 产品不存在
     * -2 用户不存在
     * -3 当前用户登录失效
     * 成功则返回产品当前的喜爱度
     */
    function add_fav(){
        echo $this->productAction->addFav($pid, $uid);
    }

    
}
